package edu.lingnan.house.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.lingnan.house.mapper.TbAgentMapper;
import edu.lingnan.house.mapper.TbHouseMapper;
import edu.lingnan.house.mapper.TbPictureMapper;
import edu.lingnan.house.pojo.TbAgent;
import edu.lingnan.house.pojo.TbHouse;
import edu.lingnan.house.pojo.TbPicture;
import edu.lingnan.house.service.RentHouseService;
import edu.lingnan.house.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RentHouseServiceImp implements RentHouseService {
    @Autowired(required = false)
    private TbHouseMapper tbHouseMapper;
    @Autowired(required = false)
    private TbPictureMapper tbPictureMapper;
    @Autowired (required = false)
    private TbAgentMapper tbAgentMapper;
    @Override
    public Result selectRentHouse(Integer minArea,Integer maxArea,Double minPrice,Double maxPrice,String address,Integer roomSum,Integer sellType) {
        QueryWrapper<TbHouse> wrapper = new QueryWrapper<>();
        if (minArea!=null)wrapper.gt("area",minArea);
        if (maxArea!=null)wrapper.le("area",maxArea);
        if (minPrice!=null)wrapper.gt("price",minPrice);
        if (maxPrice!=null)wrapper.le("price",maxPrice);
        if (address!=null||"".equals(address))wrapper.like("address",address);
        if (roomSum!=null)wrapper.eq("room_sum",roomSum);
        wrapper.in("sell_type",1,3);
        if (sellType!=null)wrapper.eq("sell_type",sellType);
        wrapper.eq("state",2);
        List<TbHouse> tbHouses = tbHouseMapper.selectList(wrapper);
        for (TbHouse tbHouse : tbHouses) {
            QueryWrapper<TbPicture> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("house_id",tbHouse.getId());
            List<TbPicture> tbPicture = tbPictureMapper.selectList(wrapper1);
            tbHouse.setTbPicture(tbPicture.get(0));
            TbAgent tbAgent = tbAgentMapper.selectById(tbHouse.getAgentId());
            tbHouse.setTbAgent(tbAgent);
        }
        int sum=tbHouses.size();
        HashMap<String, Object> map = new HashMap<>();
        map.put("house_sum",tbHouses.size());
        map.put("tbHouses",new ArrayList<>(tbHouses));
        //价格排序
        myPriceSort(tbHouses);
        map.put("tbHouse_price", new ArrayList<>(tbHouses));
        //时间排序
        myTimeSort(tbHouses);
        map.put("tbHouse_time", new ArrayList<>(tbHouses));
        return new Result(200,map,"查询成功");
    }
    //价格排序方法
    public static void myPriceSort(List<TbHouse>tbHouses) {
        Collections.sort(tbHouses, new Comparator<TbHouse>() {
            @Override
            public int compare(TbHouse o1, TbHouse o2) {
                //第一个参数属性 - 第二个参数属性 = 升序排序
                //第二个参数属性 - 第一个参数属性 = 降序排序
                Double num = o2.getPrice() - o1.getPrice();
                if (num > 0) {
                    return -1;
                } else if (num < 0) {
                    return 1;
                }
                return 0;
            }
        });
    }
    //时间排序方法
    public  static void myTimeSort(List<TbHouse>tbHouses) {
        Collections.sort(tbHouses, new Comparator<TbHouse>() {
            @Override
            public int compare(TbHouse o1, TbHouse o2) {
                //第一个参数属性 - 第二个参数属性 = 升序排序
                //第二个参数属性 - 第一个参数属性 = 降序排序
                Boolean num = o2.getCreateTime().isBefore(o1.getCreateTime());
                if (num ) {
                    return 1;
                } else if (!num ) {
                    return -1;
                }
                return 0;
            }
        });
    }
}
